﻿(function ($) {
    $.fn.extend({
        filter_inputDateTime: function (format) {
            var inputAll = $(this);
            $.each(inputAll, function (k, input) {
                $(input).mask(toMaskedFormat(format));
                $(input).change(function () {
                    var id = '#h' + input.id.toString();
                    // ReFormat DateTime to yyyy/mm/dd format
                    $(id).val(getDateTimeDefaultFormat(format, $(this).val()));
                    // Regular Check Valid Datime
                    var string = $(id).val();

                    if (string == null || string == '____/__/__')
                        string = '';
                    var word = /^(19|20)\d\d[-\/.](((0[13578]|(10|12))[-\/.](0[1-9]|[1-2][0-9]|3[0-1]))|(02[-\/.](0[1-9]|[1-2][0-9]))|((0[469]|11)[-\/.](0[1-9]|[1-2][0-9]|30)))$/ //regular expression defining datetime yyyy/mm/dd
                    if (string != '' && string.search(word) == -1) //if match failed
                    {
                        jAlert('Invalid DateTime', 'Message');
                        $(this).attr("value", "");
                        $(id).val('');
                    }
                    // ------Regular Check Valid Datime

                    $('form').load();
                });
                $(function () {
                    var id = '#h' + input.id.toString();
                    $(id).val(getDateTimeDefaultFormat(format, $(input).val()));
                });
            });
        }
    });
})(jQuery);

$(function () {
    $(".DateTime").filter_inputDateTime(DateTimeFormat); //DateTimeFormat get from session and set in master page

});

function Action(id, editingID) {
    var checked = $("input[group='Selected']:checked").length;
    if (checked == 0) {
        jAlert('Please Select Items', 'Message');
    }
    else {

        $('.required').removeAttr('min max');
        $('.required').removeClass('required');

        if (id == 'Delete') {
            jConfirm('Are you sure to delete those items?', 'Warning', function (r) {
                if (r) {
                    $('#Action').attr('value', 'Delete');
                    //$('form').validate().cancelSubmit = true;
                    $('form').submit();
                }
            });

        }
        else if (id == 'Confirm') {
            jConfirm('Are you sure to confirm those items?', 'Warning', function (r) {
                if (r) {
                    $('#Action').attr('value', 'Confirm');
                    //$('form').validate().cancelSubmit = true;
                    $('form').submit();
                }
            });
        }
        else if (id == 'UnJoin') {
            jConfirm('Are you sure to unjoin this project?', 'Warning', function (r) {
                if (r) {
                    $('#Action').attr('value', 'UnJoin');
                    //$('form').validate().cancelSubmit = true;
                    $('form').submit();
                }
            });
        }
        else if (id == 'Print') {
            $('#Action').attr('value', 'Print');
            //$('form').validate().cancelSubmit = true;
            $('form').submit();
        }
        else if (id == 'Show') {
            jConfirm('Are you sure to show references?', 'Warning', function (r) {
                if (r) {
                    $('#Action').attr('value', 'Show');
                    $('form').submit();
                }
            });
        }
        else if (id == 'Hide') {
            jConfirm('Are you sure to hide references?', 'Warning', function (r) {
                if (r) {
                    $('#Action').attr('value', 'Hide');
                    $('form').submit();
                }
            });
        }
        else if (id == 'Download') {
            $('#Action').attr('value', 'Download');
            $('form').submit();
        }
    }
}

function CheckDate(fromDate, toDate, canEqual, message) {
    $('#' + toDate).die('change');
    $('#' + toDate).live('change', function () {
        var finishDate = Date.parse($('#h' + toDate).val());
        var beginDate = Date.parse($('#h' + fromDate).val());
        
        if (!isNaN(finishDate)) {
            if (canEqual) {
                if ((finishDate - beginDate) < 0) {
                    if (message != null)
                        jAlert(message, 'Message');
                    else
                        jAlert('Start Date larger than End Date', 'Message');
                    $('#' + toDate).val('');
                    $('#h' + toDate).val('');
                    $('form').load();
                }
            }
            else {
                if ((finishDate - beginDate) <= 0) {
                    if (message != null)
                        jAlert(message, 'Message');
                    else
                        jAlert('Start Date larger than End Date', 'Message');
                    $('#' + toDate).val('');
                    $('#h' + toDate).val('');
                    $('form').load();
                }
            }
        }
    });
    $('#' + fromDate).die('change');
    $('#' + fromDate).live('change', function () {
        var finishDate = Date.parse($('#h' + toDate).val());
        var beginDate = Date.parse($('#h' + fromDate).val());
        if (!isNaN(beginDate)) {
            if (canEqual) {
                if ((finishDate - beginDate) < 0) {
                    if (message != null)
                        jAlert(message, 'LBL_Message');
                    else
                        jAlert('Start Date larger than End Date', 'Message');
                    $('#' + fromDate).val('');
                    $('#h' + fromDate).val('');
                    $('form').load();
                }
            }
            else {
                if ((finishDate - beginDate) <= 0) {
                    if (message != null)
                        jAlert(message, 'LBL_Message');
                    else
                        jAlert('Start Date larger than End Date', 'Message');
                    $('#' + fromDate).val('');
                    $('#h' + fromDate).val('');
                    $('form').load();
                }
            }
        }
    });
};

function CheckMinMax(minData, maxData, canEqual) {
    var maxValue = 0;
    var minValue = 0;
    $('#' + maxData).change(function () {
        maxValue = parseFloat($('#' + maxData).val().replace(/,/g, ""));
        minValue = parseFloat($('#' + minData).val().replace(/,/g, ""));

        if (isNaN(maxValue))
            return;
        if (isNaN(minValue))
            return;

        if (canEqual != null) {
            if ((maxValue - minValue) <= 0) {
                jAlert('GUI_VAL_MaxMin', 'LBL_Message');
                $('#' + maxData).val('');
                $('form').load();
            }
        }
        else {
            if ((maxValue - minValue) < 0.01) {
                jAlert('GUI_VAL_MaxMin', 'LBL_Message');
                $('#' + maxData).val('');
                $('form').load();
            }
        }
    });
    $('#' + minData).change(function () {
        maxValue = parseFloat($('#' + maxData).val().replace(/,/g, ""));
        minValue = parseFloat($('#' + minData).val().replace(/,/g, ""));

        if (isNaN(maxValue))
            return;
        if (isNaN(minValue))
            return;

        if (canEqual != null) {
            if ((maxValue - minValue) <= 0) {
                jAlert('GUI_VAL_MaxMin', 'LBL_Message');
                $('#' + minData).val('');
                $('form').load();
            }
        }
        else {
            if ((maxValue - minValue) < 0.01) {
                jAlert('GUI_VAL_MaxMin', 'LBL_Message');
                $('#' + minData).val('');
                $('form').load();
            }
        }
    });
};

function DoAdd() {
    if ($('#OldAction').val() == 'Edit' || $('#OldAction').val() == 'Replicate') {
        return jConfirm('GUI_CFM_ChangeAdd', 'LBL_Warning', function (r) {
            if (r) {
                $('#OldAction').val('Add');
                $('#Action').val('');
                $('.required').removeAttr('min max');
                $('.required').removeClass('required');
                PMScookies('SelectedItem', $('input[group="Selected"]:checked').val());
                $('.hide').css('display', '');
                PMScookies('HideDetail', $('.hide').css('display'));
                $('form').submit();
            }
        });
    }
    $('#OldAction').val('Add');
    $('#Action').val('');
    $('.required').removeAttr('min max');
    $('.required').removeClass('required');
    PMScookies('SelectedItem', $('input[group="Selected"]:checked').val());
    $('.hide').css('display', '');
    PMScookies('HideDetail', $('.hide').css('display'));
    $('form').submit();
}

function DoEdit(obj) {
    if ($('input[group="Selected"]:checked').length > 1 || $('input[group="Selected"]:checked').length <= 0) {
        jAlert('GUI_VAL_SelectItem', 'LBL_Message');
        return;
    }
    if ($("#OldAction").val() == 'Add' || $("#OldAction").val() == 'Replicate') {
        return jConfirm('GUI_CFM_ChangeEdit', 'LBL_Warning', function (r) {
            if (r) {
                $('#' + obj + '').val($("input[group='Selected']:checked").val());
                $('#OldAction').val('Edit');
                $('#Action').val('');
                $('.required').removeAttr('min max');
                $('.required').removeClass('required');
                PMScookies('SelectedItem', $("input[group='Selected']:checked").val());
                $('.hide').css('display', '');
                PMScookies('HideDetail', $('.hide').css('display'));
                $('form').submit();
            }
        });
    }

    $('#' + obj + '').val($("input[group='Selected']:checked").val());
    $('#OldAction').val('Edit');
    $('#Action').val('');
    $('.required').removeAttr('min max');
    $('.required').removeClass('required');
    PMScookies('SelectedItem', $("input[group='Selected']:checked").val());
    $('.hide').css('display', '');
    PMScookies('HideDetail', $('.hide').css('display'));
    $('form').submit();
}
function DoReplicate(obj) {
    if ($('input[group="Selected"]:checked').length > 1 || $('input[group="Selected"]:checked').length <= 0) {
        jAlert('GUI_VAL_SelectItem', 'LBL_Message');
        return;
    }
    if ($("#OldAction").val() == 'Add' || $('#OldAction').val() == 'Edit') {
        return jConfirm('GUI_CFM_ChangeReplicate', 'LBL_Warning', function (r) {
            if (r) {
                $('#' + obj + '').val($("input[group='Selected']:checked").val());
                $('#OldAction').val('Replicate');
                $('#Action').val('');
                $('.required').removeAttrs('min max');
                $('.required').removeClass('required');
                PMScookies('SelectedItem', $("input[group='Selected']:checked").val());
                $('.hide').css('display', '');
                PMScookies('HideDetail', $('.hide').css('display'));
                $('form').submit();
            }
        });
    }
    $('#' + obj + '').val($("input[group='Selected']:checked").val());
    $('#OldAction').val('Replicate');
    $('#Action').val('');
    $('.required').removeAttrs('min max');
    $('.required').removeClass('required');
    PMScookies('SelectedItem', $("input[group='Selected']:checked").val());
    $('.hide').css('display', '');
    PMScookies('HideDetail', $('.hide').css('display'));
    $('form').submit();
};

function Hide() {
    if ($('#OldAction').val() == 'Add' || $("#OldAction").val() == 'Edit' || $("#OldAction").val() == 'Replicate') {
        jAlert('GUI_VAL_OnActionCanNotHide', 'LBL_Message');
    }
    else {
        PMScookies('HideDetail', 'none');
        $('.hide').css('display', PMScookies('HideDetail'));
    }
}
//#region : Check Valid Data
function IsValidData(TableID, FieldID, Val) {
    var valid = true;
    if (TableID != '') {
        $('#' + TableID).find("input[group='Selected']:checked").each(function () {
            if ($('#' + FieldID + $(this).val()).val() != Val) {
                jAlert('GUI_VAL_InvalidData');
                valid = false;
            }
        });
    }
    else {
        $("input[group='Selected']:checked").each(function () {
            if ($('#' + FieldID + $(this).val()).val() != Val) {
                jAlert('GUI_VAL_InvalidData');
                valid = false;
            }
        });
    }
    return valid;
}
//*endregion

$(".DateTime").datepicker({
    dateFormat: 'dd/mm/yy',
    changeMonth: true,
    changeYear: true,
    showStatus: true,
    showWeeks: true,
    highlightWeek: true,
    numberOfMonths: 1,
    showAnim: "scale",
    showOptions: {
        origin: ["top", "left"]
    }
});